package controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import entity.School;
import entity.Teacher;
import exception.BaseException;
import factory.ServiceFactory;
import service.SchoolService;
import service.TeacherService;
import util.Condition;
import util.ControllerHelper;
import util.JSONUtil;
import util.Pagination;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

@WebServlet("/schoolCreate")
public class SchoolCreateCtl extends HttpServlet {
    private SchoolService schoolService= SchoolService.getInstance();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException{
        JSONObject message=new JSONObject();
        resp.setContentType("application/json;charset=utf-8");
        String school_json= JSONUtil.getJSON(req);
        School schoolToAdd = JSON.parseObject(school_json, School.class);

        if(schoolToAdd==null){
            message.put("message","未收到信息");
            resp.getWriter().println(message);
            return;
        }
        try{
            schoolService.add(schoolToAdd);
            message.put("状态","添加成功");
            resp.getWriter().println(message);
        }catch (BaseException e){
            message.put("message",e.getMessage());
            resp.getWriter().println(message);
            e.printStackTrace();
        }catch (SQLException e){
            message.put("message","数据库异常");
            resp.getWriter().println(message);
            e.printStackTrace();
        }catch (Exception e){
            message.put("message","网络异常");
            resp.getWriter().println(message);
            e.printStackTrace();
        }
    }
}
